#!/usr/bin/env node
//告诉操作系统我执行自定义命令的时候，使用node来执行这个文件
console.log("test");
import { program } from "commander";
import fs from "node:fs";
import inquirer from "inquirer";
import { checkPath, downloadTemplate } from "./util.js";
let json = fs.readFileSync("./package.json");
json = JSON.parse(json);
program.version(json.version);
program
  .command("create <projectName>")
  .alias("c")
  .description("创建项目")
  .action((name) => {
    console.log("create", name);
    inquirer
      .prompt([
        {
          type: "input", //输入 input：输入框 confirm:确认框 list：选择框 choice：单选框 checkbox:多选框
          name: "projectName", //名字
          message: "请输入项目名称", //提示
          default: name, //默认值
        },
        {
          type: "confirm",
          name: "isTs",
          message: "是否使用typescript",
        },
      ])
      .then((res) => {
        if (checkPath(res.projectName)) {
          console.log("项目已存在");
          return;
        }
        if (res.isTs) {
          downloadTemplate("ts", res.projectName);
        } else {
          downloadTemplate("js", res.projectName);
        }
        console.log(res, "res"); //{ projectName: 'cqj-cli', isTs: true }
      });
  });
program.parse(process.argv);
